package com.example.service.impl;

import com.example.mapper.RegisterMapper;
import com.example.pojo.RegisterMsg;
import com.example.pojo.Result;
import com.example.service.RegisterService;
import com.example.utils.PasswordSecurity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class RegisterImpl implements RegisterService {

    @Autowired
    private RegisterMapper registerMapper;

    @Override
    public Result register(RegisterMsg registerMsg) {
        try {
            if(registerMsg.getRole()==1){
                registerMsg.setPassword(PasswordSecurity.hashPassword(registerMsg.getPassword()));
                registerMapper.registerStudent(registerMsg);
            }else if(registerMsg.getRole()==2){
                registerMsg.setPassword(PasswordSecurity.hashPassword(registerMsg.getPassword()));
                registerMapper.registerTeacher(registerMsg);
            }
            return Result.success("注册成功");
        }catch (Exception e){
            String  message = e.getMessage();
            log.error(message);
            if(message.contains("'class_id' cannot be null")) {
                return Result.fail("注册失败,请检查班级是否存在");
            }
            return Result.fail(message);
        }
    }
}
